package com.bsoft.gol.hbs.dao;

import com.bsoft.gol.hbs.utils.CommonUtils;
import ctd.controller.exception.ControllerException;
import ctd.persistence.annotation.DAOMethod;
import ctd.persistence.annotation.DAOParam;
import ctd.persistence.support.hibernate.template.HibernateSessionTemplate;
import ctd.persistence.support.hibernate.template.HibernateStatelessAction;
import hcn.base.MsgTemplate;
import org.atag.dao.util.BaseDaoImpl;
import org.hibernate.StatelessSession;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import pcn.bean.consultation.MsgManageListQo;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @describe 消息管理 Dao层
 */
public abstract class MsgTemplateDao extends BaseDaoImpl<MsgTemplate> {
    private static final Logger logger = LoggerFactory.getLogger(MsgTemplateDao.class);

    public MsgTemplateDao() {
        this.setKeyField("msgTemplateId");
    }

    @DAOMethod(sql = "from MsgTemplate where msgTemplateId=:msgTemplateId")
    public abstract MsgTemplate getMsgTemplateId(@DAOParam("msgTemplateId") Long msgTemplateId);

    @DAOMethod(sql = "from MsgTemplate where templateCode=:templateCode and tenantId=:tenantId ")
    public abstract MsgTemplate getMsgTemplateCodeAndTenantId(@DAOParam("templateCode") String msgTemplateCode,@DAOParam("tenantId") String tenantId);

    @DAOMethod(sql = "from MsgTemplate where notificateType=:notificateType and tenantId=:tenantId ")
    public abstract MsgTemplate getMsgNotificateTypeAndTenantId(@DAOParam("notificateType") String notificateType,@DAOParam("tenantId") String tenantId);

    @DAOMethod(sql = "from MsgTemplate where smsserviceType=:smsserviceType and tenantId=:tenantId ")
    public abstract MsgTemplate getMsgBySmsServiceTypeAndTenantId(@DAOParam("smsserviceType") String notificateType, @DAOParam("tenantId") String tenantId);

    /**
     * @throws
     * @describe 获取消息管理模板列表
     * @author zhengcb10708
     * @date 2020/9/18 11:13
     */
    public List<MsgTemplate> getMsgTemplateList(final MsgManageListQo qo) {
        Map<String, Object> params = new HashMap<>();

        StringBuffer stringBuffer = new StringBuffer();
        stringBuffer.append(" select msg_template_id msgTemplateId , template_name templateName, tenant_id tenantId ,msg_title msgTitle, template_content templateContent, template_code templateCode, status,  \n" +
                "    link_type linkType, link_target linkTarget, notice_type noticeType, sms_template_id smsTemplateId , sms_jump_url smsJumpUrl, sms_parameter_list smsParameterList,  \n" +
                "    create_user_id createUserId, create_date createDate, last_modified_user_id lastModifiedUserId, last_modified_date lastModifiedDate \n" +
                "     from msg_template a   where 1=1   ");

        if (CommonUtils.isNotEmpty(qo.getKeyWord())) {
            stringBuffer.append(" and a.template_name like :templateName  ");
            params.put("templateName", "%" + qo.getKeyWord() + "%");
        }

        if (CommonUtils.isNotEmpty(qo.getTenantId())) {
            stringBuffer.append("  and   a.tenant_id =:tenantId  ");
            params.put("tenantId", qo.getTenantId());
        }
        stringBuffer.append("  order by a.msg_template_id asc  ");
        return super.findListBySql(stringBuffer.toString(), params, MsgTemplate.class, qo.getPageNo(), qo.getPageSize());
    }

    /**
     * @return
     * @throws
     * @describe 查询消息模板总数
     * @author zhengcb10708
     * @date 2020/9/18 10:19
     */
    public Long getMsgTemplateCount(final MsgManageListQo msgManageListQo) {
        Map<String, Object> params = new HashMap<>();
        StringBuffer hsql = new StringBuffer();
        hsql.append("select count(1)  from msg_template  a  ");
        hsql.append("  where 1=1 ");

        if (msgManageListQo.getKeyWord() != null && !msgManageListQo.getKeyWord().isEmpty()) {
            hsql.append(" and a.template_name like :templateName ");
            params.put("templateName", "%" + msgManageListQo.getKeyWord() + "%");
        }
        if (msgManageListQo.getTenantId() != null && !msgManageListQo.getTenantId().isEmpty()) {
            hsql.append("  and  a.tenant_id = :tenantId ");
            params.put("tenantId", msgManageListQo.getTenantId());
        }
        return super.findForLongBySql(hsql.toString(), params);
    }

    /**
     * @return
     * @throws
     * @describe 查询消息code查询模板总数
     * @author zhengcb10708
     * @date 2020/9/18 10:19
     */
    public Long getMsgTemplateCountByTmpCode(final String templateCode) {
        Map<String, Object> params = new HashMap<>();
        StringBuffer hsql = new StringBuffer();
        hsql.append("select count(1)  from msg_template  a  ");
        hsql.append("  where 1=1 ");
        if (templateCode != null && !templateCode.isEmpty()) {
            hsql.append(" and  a.template_code = :templateCode");
            params.put("templateCode", templateCode);

        }
        return super.findForLongBySql(hsql.toString(), params);
    }

    /**
     * @return
     * @throws
     * @describe 查询消息code和租户id查询模板总数
     * @author zhengcb10708
     * @date 2020/9/18 10:19
     */
    public Long getMsgTemplateCount(final String templateCode, final String tenantId, final Long msgTemplateId) {
        Map<String, Object> params = new HashMap<>();

        StringBuffer hsql = new StringBuffer();
        hsql.append("select count(1)  from msg_template  a  ");
        hsql.append("  where 1=1 ");
        if (templateCode != null && !templateCode.isEmpty()) {
            hsql.append(" and  a.template_code = :templateCode");
            params.put("templateCode", templateCode);
        }
        if (tenantId != null && !tenantId.isEmpty()) {
            hsql.append(" and  a.tenant_id = :tenantId ");
            params.put("tenantId", tenantId);

        }
        if (msgTemplateId.intValue() > 0) {
            hsql.append(" and  a.msg_template_id <> :msgTemplateId");
            params.put("msgTemplateId", msgTemplateId);
        }
        return super.findForLongBySql(hsql.toString(), params);
    }

    /**
     *
     *@describe 新增消息模板
     *@throws
     *@author zhengcb10708
     *@date 2020/9/21 14:14
     */
    public String addMsgTemplate(final MsgTemplate msgTemplate) throws ControllerException {
        HibernateSessionTemplate.instance().execute(new HibernateStatelessAction() {
            @Override
            public void execute(StatelessSession ss) throws Exception {
                ss.insert(msgTemplate);
            }
        });
        return msgTemplate.getTemplateCode();
    }
    /**
     *
     *@describe 修改消息模板
     *@throws
     *@author zhengcb10708
     *@date 2020/9/21 14:14
     */
    public String updateMsgTemplate(final MsgTemplate msgTemplate) throws ControllerException {
        HibernateSessionTemplate.instance().execute(new HibernateStatelessAction() {
            @Override
            public void execute(StatelessSession ss) throws Exception {
                //更新模板信息表
                ss.update(msgTemplate);
            }
        });
        return msgTemplate.getTemplateCode();
    }

}
